package cn.edu.sgu.www.mhxysy.service.jingmai.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.jingmai.StarStoneDTO;
import cn.edu.sgu.www.mhxysy.entity.jingmai.StarStone;
import cn.edu.sgu.www.mhxysy.entity.jingmai.StarStoneCategory;
import cn.edu.sgu.www.mhxysy.enums.InlayStatus;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.jingmai.StarStoneCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.jingmai.StarStoneMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.jingmai.StarStonePager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.jingmai.StarStoneService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class StarStoneServiceImpl implements StarStoneService {

    private final StarStoneMapper starStoneMapper;
    private final StarStoneCategoryMapper starStoneCategoryMapper;

    @Autowired
    public StarStoneServiceImpl(
            StarStoneMapper starStoneMapper,
            StarStoneCategoryMapper starStoneCategoryMapper) {
        this.starStoneMapper = starStoneMapper;
        this.starStoneCategoryMapper = starStoneCategoryMapper;
    }

    @Override
    public void insert(StarStoneDTO starStoneDTO) {
        StarStone starStone = toEntity(starStoneDTO);

        if (starStone != null) {
            starStone.setId(starStone.uuid());
            starStone.setInlayStatus(InlayStatus.WXQ.getValue());

            starStoneMapper.insert(starStone);
        }
    }

    @Override
    public void deleteById(String id) {
        starStoneMapper.deleteById(id);
    }

    @Override
    public void updateById(StarStoneDTO starStoneDTO) {
        ObjectUtils.requireNonNull(starStoneDTO.getId(), CustomMessages.ID_NOT_NULL);

        StarStone starStone = toEntity(starStoneDTO);

        starStoneMapper.updateById(starStone);
    }

    @Override
    public Page<StarStone> selectByPage(StarStonePager pager) {
        QueryWrapper<StarStone> queryWrapper = new QueryWrapper<>();
        Page<StarStone> page = Pager.ofPage(pager);

        queryWrapper.eq(
                StringUtils.isNotEmpty(pager.getRoleId()),
                "role_id", pager.getRoleId()
        );
        queryWrapper.eq(
                pager.getCategoryId() != null,
                "category_id", pager.getCategoryId()
        );

        return starStoneMapper.selectPage(page, queryWrapper);
    }

    private StarStone toEntity(StarStoneDTO stoneDTO) {
        boolean result = validateQuality(stoneDTO);

        if (result) {
            StarStone stone = new StarStone();

            stone.setId(stoneDTO.getId());
            stone.setDays(stoneDTO.getDays());
            stone.setState(stoneDTO.getState());
            stone.setRoleId(stoneDTO.getRoleId());
            stone.setQuality(stoneDTO.getQuality());
            stone.setCategoryId(stoneDTO.getCategoryId());

            return stone;
        }

        return null;
    }

    /**
     * 验证晶石品质
     * @param stoneDTO StarStoneDTO
     * @return boolean 如果晶石品质和晶石类型对应，返回true；否则返回false
     */
    private boolean validateQuality(StarStoneDTO stoneDTO) {
        StarStoneCategory stoneCategory = starStoneCategoryMapper.selectById(stoneDTO.getCategoryId());

        Short minQuality = stoneCategory.getMin();
        Short maxQuality = stoneCategory.getMax();

        if (stoneDTO.getQuality() > maxQuality || stoneDTO.getQuality() < minQuality) {
            String message = "晶石品质不正确：" + stoneCategory.getName() + "的品质范围为" + minQuality + "~" + maxQuality;

            throw new GlobalException(ResponseCode.BAD_REQUEST, message);
        }

        return true;
    }

}